package com.jhws.sys.menu.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.jhws.core.db.BaseDao;
import com.jhws.sys.menu.bo.MenuConstant;
import com.jhws.sys.menu.bo.TblMenuDimBo;
import com.jhws.sys.role.bo.TblRolemenuDimBo;
import com.jhws.sys.menu.bo.MenuObject;
import com.jhws.common.base.bo.Global;
import com.jhws.sys.role.service.ITblRolemenuDimService;
import com.jhws.sys.user.service.ITblUserDimService;

public class TblMenuDimServiceSpringImpl implements ITblMenuDimService {
	@Resource
	private BaseDao BaseDao;

	@Resource
	ITblUserDimService IUserService;
	@Resource
	ITblRolemenuDimService IRoleMenuService;


	public void save(TblMenuDimBo bo) throws Exception {
		BaseDao.saveOrUpdate(bo);
	}

	public List<TblMenuDimBo> findTblMenuDimByWhere(String whereClause) throws Exception {
		return BaseDao.findList("from TblMenuDimBo "+whereClause+" order by menu_index asc");
	}

	public TblMenuDimBo findTblMenuDimById(String TblMenuId) {
		return BaseDao.get(TblMenuDimBo.class,TblMenuId);
	}

	public void removeMenuDim(String TblMenuId) {
		BaseDao.delete(TblMenuDimBo.class,TblMenuId);
	}


    public String getMenuString(String url) throws Exception {
		
		String strutl="";
		List<TblMenuDimBo> list = BaseDao.findList("from TblMenuDimBo where menu_url like'%"+ url+"%'");
		if(list!=null&&list.size()>0){
			TblMenuDimBo bo = (TblMenuDimBo)list.get(0);
			strutl="&menuPare="+bo.getMenuPar()+"&menuChild="+bo.getMenuCode();
		}
		return strutl;
	}

	private boolean checkRoleForMenu(List<TblRolemenuDimBo> roleMenuList,MenuObject obj,int type) {
		if(type == Global.TYPE_ALL_MENUS)
			return true;
		for(TblRolemenuDimBo roleMenu : roleMenuList) {
			if(roleMenu.getMenuCode().equals(obj.getSelf().getMenuCode()) ){
				obj.setChecked(1);
				return true;
			}
		}
		
		if(type==Global.TYPE_ALL_MENUS_WITH_ROLE_CHECK)
			return true;
		return false;
	}

	/* 获取所有菜单的MenuObject列表
	 *  @level - 从哪一级菜单开始查找
	 *  @lastMenuList - 外部传参总是为null, 因为它是内部递归时使用的
	 *  @roleMenuList - 根据type来决定是否传null
	 *  @type -  TYPE_ALL_MENUS : 获取所有菜单,不区分role，roleMenuList为null
	 *  	  -  TYPE_ROLE_MENUS : 获取某个角色的所有菜单，上面的roleMenuList需要传入该角色的菜单ID列表
	 *  	  -  TYPE_ALL_MENUS_WITCH_ROLE_CHECK : 获取所有菜单，并将某个角色的菜单做上标记，角色菜单分配时专用
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MenuObject> getAllMenuObjects(int level,List<TblMenuDimBo> lastMenuList, 
												List<TblRolemenuDimBo> roleMenuList,int type) throws Exception {
	
		List<TblMenuDimBo> menuList = findTblMenuDimByWhere(
				" where menu_lev='"+level+"' and type = '"+ MenuConstant.TYPEMENU +"'");
		if(menuList == null || menuList.size() == 0){
				return null;
		}
		
		List<MenuObject> objList = getAllMenuObjects(level + 1, menuList,roleMenuList,type);
		/* 递归结束， 如果返回null,这里的menuList就是最后一级菜单集合，
		 * lastMenuList是倒数第二次菜单集合,需要组成object 返回*/
		if(objList == null) {
			List<MenuObject> menuObjList = new ArrayList<MenuObject>();
			for(TblMenuDimBo bo : lastMenuList){		
				MenuObject firstObj = new MenuObject();
				firstObj.setSelf(bo);
				firstObj.setSubMenus(new ArrayList<MenuObject>());
				/* 检测角色中是否拥有此菜单 */
				if(!checkRoleForMenu(roleMenuList,firstObj,type)) 
						continue;
		
				menuObjList.add(firstObj);
				for(TblMenuDimBo secBo : menuList) {
					if(secBo.getMenuPar().equals(bo.getMenuCode())){
						MenuObject secObj = new MenuObject();
						secObj.setSelf(secBo);
						secObj.setSubMenus(new ArrayList<MenuObject>());
						if(!checkRoleForMenu(roleMenuList,secObj,type))
								continue;					
						firstObj.getSubMenus().add(secObj);
					}	
				}
			}
			return menuObjList;
		}
		/* 如果返回的objectList, 则把它封装到lastMenuList中去 */
		else if(lastMenuList != null) {
			List<MenuObject> menuObjList = new ArrayList<MenuObject>();
			for(TblMenuDimBo bo : lastMenuList){	
				MenuObject firstObj = new MenuObject();
				firstObj.setSelf(bo);
				firstObj.setSubMenus(new ArrayList<MenuObject>());
				if(!checkRoleForMenu(roleMenuList,firstObj,type))
						continue;
	
				menuObjList.add(firstObj);
				for(MenuObject obj : objList) {
					if(obj.getSelf().getMenuPar().equals(bo.getMenuCode())){
						firstObj.getSubMenus().add(obj);
					}
				}
			}
			return menuObjList;
		}
		else
			return objList;
	}

}
